#include "threadPool.h"

void *threadFunc(void *arg){
    threadPool_t * pthreadPool = (threadPool_t *)arg;
    pthread_mutex_lock(&pthreadPool->mutex);
    MYSQL *mysql=mysql_init(NULL);
    connectmysql(mysql);
    pthread_mutex_unlock(&pthreadPool->mutex);
    while(1){
        int netfd;
        pthread_mutex_lock(&pthreadPool->mutex);
        //        pthread_cleanup_push(unlock,pthreadPool);
        while(pthreadPool->taskQueue.queueSize == 0 && pthreadPool->exitFlag == 0){
            pthread_cond_wait(&pthreadPool->cond, &pthreadPool->mutex);
        }
        if(pthreadPool->exitFlag == 1){
            printf("I am worker. I am going to exit!\n");
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        netfd = pthreadPool->taskQueue.pFront->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //pthread_cleanup_pop(1);
        printf("I am worker! I got netfd = %d\n", netfd);
        //这里写函数
        char file_store_path[]="server_storage";
        int cmd=-1;
        int length=-1;//传参的长度
        int len=-1;//专门处理最后发送
        char addr[1000]={0};
        /* char now_addr[1024]={"."}; */
        int user_base_addr;
        char buf[4096]={0};
        int ret=-1;
        char username[100]={0};
        char errbuf[1000]={0};
        char virtualPath[500]={0};
        //char filename[100];
        while(1)
        {
            cmd=-1;
            //sleep(1);
            bzero(buf,sizeof(buf));
            recvCommandPacket(netfd,&cmd,&length,addr);
            if(length==0){
                length=1;
                memcpy(addr," ",1);
            }
            //sleep(2);
            /* printf("cmd=%d\n",cmd); */
            //printf("length=%d\n",length);
            switch(cmd)
            {
            case 0://cd
                printf("cd\n");
                ret=cdFunc(username,addr,strlen(addr),&user_base_addr,mysql,buf);
                send(netfd,&ret,sizeof(int),MSG_WAITALL);
                if(ret==-1)
                {
                    strcpy(buf,"cd failed\n");
                    len=strlen(buf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,buf,len,MSG_WAITALL);
                }
                /* printf("cd finish\n"); */
                break;
            case 1://ls
                printf("ls\n");
                ret=lsFunc(username,addr,strlen(addr),user_base_addr,buf,mysql,errbuf);
                /* printf("ret=%d\n",ret); */
                send(netfd,&ret,sizeof(int),MSG_WAITALL);
                len=strlen(buf);
                send(netfd,&len,sizeof(int),MSG_WAITALL);
                send(netfd,buf,len,MSG_WAITALL);
                /* printf("ls finish\n"); */
                break;
            case 2://pwd
                printf("pwd\n");
                ret=pwdFunc(mysql,user_base_addr,virtualPath,errbuf);
                send(netfd,&ret,sizeof(int),MSG_WAITALL);
                len=strlen(virtualPath);
                send(netfd,&len,sizeof(int),MSG_WAITALL);
                send(netfd,virtualPath,len,MSG_WAITALL);
                /* printf("pwd finish\n"); */
                break;
            case 3://mkdir
                   //这里的addr，等于文件名,后面的都一样
                printf("mkdir\n");
                ret=mkdirFunc(mysql,user_base_addr,addr,username,errbuf);
                send(netfd,&ret,sizeof(int),MSG_WAITALL);
                if(ret==-1)
                {
                    len=strlen(errbuf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,errbuf,len,MSG_WAITALL);
                }
                /* printf("mkdir finish\n"); */
                break;
            case 4://rmdir
                printf("rmdir\n");
                ret=rmdirFunc(mysql,user_base_addr,addr,errbuf);
                send(netfd,&ret,sizeof(int),MSG_WAITALL);
                if(ret==-1)
                {
                    /* printf("rmdir enter!\n"); */
                    len=strlen(errbuf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,errbuf,len,MSG_WAITALL);
                }
                /* printf("rmdir finish\n"); */
                break;
            case 5://puts 上传
                printf("puts\n");
                ret=getsfunc(netfd,user_base_addr,mysql);//puts
                                         //ret=putsFunc(netfd,addr);
                if(ret==-1)
                {
                    strcpy(buf,"puts failed\n");
                    len=strlen(buf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,buf,len,MSG_WAITALL);
                }
                /* printf("puts finish\n"); */
                break;
            case 6://gets
                printf("gets\n");
                ret=putsfunc(netfd,user_base_addr,mysql);//puts
                                         //ret=getsFunc(netfd,addr);
                if(ret==-1)
                {
                    strcpy(buf,"gets failed\n");
                    len=strlen(buf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,buf,len,MSG_WAITALL);
                }
                else{
                    strcpy(buf,"gets success\n");
                    len=strlen(buf);
                    send(netfd,&len,sizeof(int),MSG_WAITALL);
                    send(netfd,buf,len,MSG_WAITALL);
                }
                printf("gets finish\n");
                //printf("------\n");
                break;
            case 7://remove
                ret=rmfileFunc(username,addr,strlen(addr),user_base_addr,mysql,file_store_path,errbuf);
                if(ret==0){
                    printf("success\n");
                }
                else{
                    printf("fail\n");
                }
                /* printf("rmfile finish\n"); */

                break;
            case 8://exit
                printf("exit\n");
                /* printf("exit finish\n"); */
                goto end;
            case 9://reg
                printf("reg\n");
                pthread_mutex_lock(&pthreadPool->mutex);
                regi(netfd,mysql,addr,&user_base_addr);
                pthread_mutex_unlock(&pthreadPool->mutex);
                memcpy(username,addr,strlen(addr));
                LOG_CONNECT(username,cmd);
                /* printf("register finish\n"); */
                break;
            case 10://login
                ret=login(netfd,mysql,addr,&user_base_addr);
                printf("%d\n",user_base_addr);
                if(ret==-1){
                    printf("login failed!\n");
                }
                else if(ret==0){
                    printf("login success!\n");
                    memcpy(username,addr,strlen(addr));
                    LOG_CONNECT(username,cmd);
                }
                /* printf("login finish\n"); */
                break;
            default:
                goto end;
            }
            GETLOG(username,cmd);
            if(ret==-1)
            {
                LOG_ERROR(username,errbuf);
            }
        }
end:
        close(netfd);
    }
        mysql_close(mysql);
}
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum; ++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}

